package com.leetcode.No1702;

import org.junit.Test;

public class Solution {
    public String maximumBinaryString(String binary) {
        int n = binary.length();
        if (n == 2) {
            return binary;
        }

        char[] chars = binary.toCharArray();
        int i = 0;
        int j = 1;
        while (j < n) {
            if (chars[i] == '0' && chars[j] == '0') {
                chars[i] = '1';
                i = j++;
                continue;
            }
            while (chars[i] == '0' && chars[j] == '1') {
                while (j < n && chars[j] == '1') {
                    ++j;
                }
                if (j == n) {
                    break;
                }
                if (j < n) {
                    chars[i] = '1';
                    chars[i+1] = '0';
                    chars[j] = '1';
                    ++i;
                }
            }
            i = j++;
        }
        return String.valueOf(chars);
    }

    @Test
    public void test() {
        // "00" -> "10"
        // "10" -> "01"
//        String binary = "000110";
//        String binary = "01111001100000110010";
        String binary = "1100";
        System.out.println(maximumBinaryString(binary));
    }
}
